package com.ruoyi.project.system.zuozheng.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.doctor.domain.TableDoctor;
import com.ruoyi.project.system.doctor.service.ITableDoctorService;
import com.ruoyi.project.system.register.domain.TableRegister;
import com.ruoyi.project.system.register.service.ITableRegisterService;
import com.ruoyi.project.system.zuozheng.domain.TableZuozheng;
import com.ruoyi.project.system.zuozheng.service.ITableZuozhengService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 坐诊信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-13
 */
@Controller
@RequestMapping("/system/zuozheng")
public class TableZuozhengController extends BaseController
{
    @Autowired
    private ITableDoctorService tableDoctorService;
    private String prefix = "system/zuozheng";
    @Autowired
    private ITableRegisterService tableRegisterService;
    @Autowired
    private ITableZuozhengService tableZuozhengService;

    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @GetMapping()
    public String zuozheng()
    {
        return prefix + "/zuozheng";
    }

    /**
     * 查询坐诊信息列表
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TableZuozheng tableZuozheng)
    {
        startPage();
        List<TableZuozheng> list = tableZuozhengService.selectTableZuozhengList(tableZuozheng);
        for (TableZuozheng zuozheng : list) {
            TableRegister query = new TableRegister();
            query.setDoctorName(zuozheng.getDoctorName());
            List<TableRegister> tableRegisterList = tableRegisterService.selectTableRegisterList(query);
            if (tableRegisterList.size() > 0){
                zuozheng.setGuahaoNum(new Long(tableRegisterList.size()));
                zuozheng.setGuahaoDoller(new BigDecimal(tableRegisterService.selectTableRegisterList(query).stream().mapToLong(TableRegister::getGuahaoDoller).sum()));
            }else {
                zuozheng.setGuahaoNum(new Long(0));
                zuozheng.setGuahaoDoller(new BigDecimal(0));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出坐诊信息列表
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @Log(title = "坐诊信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TableZuozheng tableZuozheng)
    {
        List<TableZuozheng> list = tableZuozhengService.selectTableZuozhengList(tableZuozheng);
        ExcelUtil<TableZuozheng> util = new ExcelUtil<TableZuozheng>(TableZuozheng.class);
        return util.exportExcel(list, "坐诊信息数据");
    }

    /**
     * 新增坐诊信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存坐诊信息
     */
    @RequiresRoles(value = {"manager"})
    @Log(title = "坐诊信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TableZuozheng tableZuozheng)
    {
        TableZuozheng query = new TableZuozheng();
        query.setDoctorName(tableZuozheng.getDoctorName());

        if(tableZuozhengService.selectTableZuozhengList(query).size()>0){
            return error("该医生已存在坐诊信息，请勿重复添加");
        }
        TableDoctor doctorQuery = new TableDoctor();
        doctorQuery.setName(tableZuozheng.getDoctorName());
        TableDoctor tableDoctor = tableDoctorService.selectTableDoctorList(doctorQuery).get(0);
        tableZuozheng.setDoctorSex(tableDoctor.getSex());
        tableZuozheng.setKeshiName(tableDoctor.getKeshi());
        tableZuozheng.setDoctorZhicheng(tableDoctor.getZhicheng());
        return toAjax(tableZuozhengService.insertTableZuozheng(tableZuozheng));
    }

    /**
     * 修改坐诊信息
     */
    @RequiresRoles(value = {"manager"})
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TableZuozheng tableZuozheng = tableZuozhengService.selectTableZuozhengById(id);
        mmap.put("tableZuozheng", tableZuozheng);
        return prefix + "/edit";
    }

    /**
     * 修改保存坐诊信息
     */
    @RequiresRoles(value = {"manager"})
    @Log(title = "坐诊信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TableZuozheng tableZuozheng)
    {
        return toAjax(tableZuozhengService.updateTableZuozheng(tableZuozheng));
    }

    /**
     * 删除坐诊信息
     */
    @RequiresRoles(value = {"manager"})
    @Log(title = "坐诊信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tableZuozhengService.deleteTableZuozhengByIds(ids));
    }
}
